package com.manager.plat.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.ILimitUserNumDao;
import com.manager.plat.vo.LimitUserNumVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class LimitUserNumAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1446544720416345169L;

	private static Logger logger = Logger.getLogger(LimitUserNumAction.class);
	
	private ILimitUserNumDao limitUserNumDao;			//用户当日拨打次数限制数据接口
	private List<LimitUserNumVo> limitUserNumVoList;	//用户当日拨打次数信息列表
	private LimitUserNumVo limitUserNumVo;				//用户当日拨打次数信息VO
	private String actionPath;							//操作成功跳转路径
	
	public String getLimitUserNumList(){
		limitUserNumVoList = limitUserNumDao.getLimitUserNumList();
		return SUCCESS;
	}
	
	public String addLimitUserNum(){
		limitUserNumVo = null;
		return SUCCESS;
	}
	
	public String editLimitUserNum(){
		if(limitUserNumVo.getId() != 0){
			limitUserNumVo = limitUserNumDao.getLimitUserNumById(limitUserNumVo.getId());
		}
		return SUCCESS;
	}
	public String updateLimitUserNum(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		limitUserNumVo.setAdd_user(platUser.getId());
		if(limitUserNumVo.getId() == 0){
			//判断用户总量限制表是否已经存在，已经存在的话就不能再次新增
			int count = limitUserNumDao.insertLimitUserNum(limitUserNumVo);
			
			if(count > 0){
				logger.info(String.format("[%s]添加用户拨打次数信息成功", platUser.getLogin_username()));
			}else{
				logger.info(String.format("[%s]添加用户拨打次数信息失败", platUser.getLogin_username()));
			}
		}else {
			int count = limitUserNumDao.updateLimitUserNum(limitUserNumVo);
			if(count > 0){
				logger.info(String.format("[%s]更新用户拨打次数信息成功", platUser.getLogin_username()));
			}else{
				logger.info(String.format("[%s]更新用户拨打次数信息失败", platUser.getLogin_username()));
			}
		}
		
		actionPath = "limitUserNum/getLimitUserNumList.action";
		return SUCCESS;
	}
	
	public String deleteLimitUserNum(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		int count = limitUserNumDao.deleteLimitUserNum(limitUserNumVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除用户拨打次数信息成功", platUser.getLogin_username()));
		}else{
			logger.info(String.format("[%s]删除用户拨打次数信息失败", platUser.getLogin_username()));
		}
		
		actionPath = "limitUserNum/getLimitUserNumList.action";
		return SUCCESS;
	}
	
	public ILimitUserNumDao getLimitUserNumDao() {
		return limitUserNumDao;
	}
	public void setLimitUserNumDao(ILimitUserNumDao limitUserNumDao) {
		this.limitUserNumDao = limitUserNumDao;
	}
	public List<LimitUserNumVo> getLimitUserNumVoList() {
		return limitUserNumVoList;
	}
	public void setLimitUserNumVoList(List<LimitUserNumVo> limitUserNumVoList) {
		this.limitUserNumVoList = limitUserNumVoList;
	}
	public LimitUserNumVo getLimitUserNumVo() {
		return limitUserNumVo;
	}
	public void setLimitUserNumVo(LimitUserNumVo limitUserNumVo) {
		this.limitUserNumVo = limitUserNumVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
